import pytest
import requests
import logging
import pymysql



class TestA3mallCart:
    token=None
    def test_dl(self):
        url="http://10.59.9.12:7001/api/public/login"
        headers={"content-type":"application/json"}
        body={"username":"13811033962","password":"123456"}
        res=requests.post(url=url,headers=headers,json=body)
        print("登录成功:",res.json())

        TestA3mallCart.token="Bearer "+res.json()['data']['token']
        logging.info(res.json())
    def test_mslb(self):
        url="http://10.59.9.12:7001/api/second?page=1&type=0&sort=1"
        headers={"auth-token":TestA3mallCart.token}
        res = requests.get(url=url, headers=headers)
        print("秒杀列表:",res.json())

    def test_spxq(self):
        url = "http://10.59.9.12:7001/api/second/view?id=5"
        headers = {"auth-token": TestA3mallCart.token}
        res = requests.get(url=url, headers=headers)
        print("商品详情", res.json())
        assert res.status_code==200
    def test_qrdd(self):
        url = "http://10.59.9.12:7001/api/order/confirm?id=5&type=second&sku_id=&num=1&shipping_type=1"
        headers = {"auth-token": TestA3mallCart.token}
        res = requests.get(url=url, headers=headers)
        print("确认订单:",res.json())
        assert res.status_code==200
    def test_zf(self):
        conn = pymysql.connect(user="root", password="yidong666", database="a3mall", host="10.59.9.12")
        cur = conn.cursor()
        cur.execute("select id from mall_order")
        oid = cur.fetchall()
        print("连接成功")
        print(oid)
        lastoid = oid[-1][0]
        url="http://10.59.9.12:7001/api/order/create"
        headers={"auth-token":TestA3mallCart.token}
        body={"id":"5","type":"second","address_id":5  ,"store_id":1,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":"http://10.59.9.12:7001/wap/pages/cart/confirm?id=5&sku_id=&num=1&type=second","sku_id":"","num":"1"}
        res = requests.post(url=url, headers=headers, json=body)
        print("支付成功:",res.json())
        assert lastoid+1==res.json()['data']['order_id']

    def test_ddlb(self):
        url = "http://10.59.9.12:7001/api/order/list?type=1&page=1"
        headers = {"auth-token": TestA3mallCart.token}
        res = requests.get(url=url, headers=headers)
        print("订单列表:",res.json())
        assert res.status_code==200